package cn.snorlaxzjt.LBSserver.mapper;

import cn.snorlaxzjt.LBSserver.po.entity.Moment;
import cn.snorlaxzjt.LBSserver.po.entity.MomentEntity;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import java.math.BigDecimal;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

@Mapper
@Repository
public interface MomentRepo extends BaseMapper<MomentEntity> {
    String TABLE_NAME = "Moment";

    @Select("select * from moment where user_id = #{friendId} and  #{curId} > id and status = 1 order by create_time desc LIMIT #{size}")
    List<Moment> queryMomentsByUserId(@Param(value = "friendId") Integer userId,@Param(value = "size")Integer size,
            @Param(value = "curId")Integer curId);

    @Select("select count(*) from moment where user_id = #{user_id} and status = 1 ")
    Integer queryCountByUserId(@Param(value = "user_id") Integer user_id);

    @Select("<script>"
            +"select * from moment where user_id  in("
            + "<foreach item='item' index='index' collection='list'  separator=','>"
            + "#{item}"
            + "</foreach>"
            +") and #{curId} > id and status = 1 order by create_time desc LIMIT #{size}"
            + "</script>")
    List<Moment> queryFriendsMomentsById(@Param(value = "list") List<Integer> friendsList,
                                         @Param(value = "size")Integer size,
                                         @Param(value = "curId")Integer curId);


    @Select("<script>"
            +"select COUNT(*) from moment where user_id  in ("
            +"<foreach collection='list' item='id' separator=','>"
            +"#{id}"
            +"</foreach>) and status = 1 "
            +"</script>")
    Integer queryFriendsMomentsCountById(@Param(value = "list") List<Integer> friendsList);

    @Select("SELECT *FROM moment where ACOS(COS(RADIANS(#{lat})) *COS(RADIANS(latitude)) "
            +"*COS(RADIANS(longitude) - RADIANS(#{lon})) +"
            +"SIN(RADIANS(#{lat})) *SIN(RADIANS(latitude))"
            +") * 6378  < #{distance} limit 100 ")
    List<Moment> queryMomentsInDis(BigDecimal lon,BigDecimal lat,Double distance);

    @Select("select image_url from moment where user_id = #{id} order by create_time desc limit 3")
    List<String> getRecentlyThreeById(Integer id);

}
